package com.example.smartrefreshlayoutdemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.smartrefreshlayoutdemo.R;
import com.example.smartrefreshlayoutdemo.activity.style.DeliveryStyleActivity;
import com.example.smartrefreshlayoutdemo.activity.style.DropBoxStyleActivity;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class RefreshStylesFragment extends Fragment {
    private RecyclerView recyclerView;
    private RefreshStyleAdapter adapter;
    private RefreshLayout refreshLayout;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_refresh_styles,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter = new RefreshStyleAdapter(Arrays.asList(Item.values())));

        refreshLayout = view.findViewById(R.id.refreshLayout);
        if (refreshLayout != null) {
            refreshLayout.setOnMultiListener(new SimpleMultiListener(){
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    refreshLayout.finishRefresh(2000);
                }

                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    refreshLayout.finishLoadMore(2000);
                }

                @Override
                public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
                    super.onStateChanged(refreshLayout, oldState, newState);
                }
            });
        }
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Class<?> clazz = Item.values()[position].clazz;
                startActivity(new Intent(getContext(),clazz));
            }
        });

    }
    public class RefreshStyleAdapter extends BaseQuickAdapter<Item, BaseViewHolder>{

        public RefreshStyleAdapter(@org.jetbrains.annotations.Nullable List<Item> data) {
            super(android.R.layout.simple_list_item_2, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, Item item) {
            holder.setText(android.R.id.text1,item.name());
            holder.setText(android.R.id.text2,item.nameId);
            holder.setTextColorRes(android.R.id.text2,R.color.colorTextAssistant);
        }
    }

    private enum Item{
        Delivery(R.string.title_activity_style_delivery, DeliveryStyleActivity.class),
        DropBox(R.string.title_activity_style_drop_box, DropBoxStyleActivity.class)
        ;
        public int nameId;
        public Class<?> clazz;

        Item(int nameId, Class<?> clazz) {
            this.nameId = nameId;
            this.clazz = clazz;
        }
    }
}
